package cn.acac.yygh.user.controller.user;

import cn.acac.yygh.model.user.Patient;
import cn.acac.yygh.result.R;
import cn.acac.yygh.user.service.PatientService;
import cn.acac.yygh.utils.JwtHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 就诊人控制器
 */
@RestController
@RequestMapping("/user/userInfo/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;


    // 增删改查

    /**
     * 增加就诊人的方法
     * @param patient
     * @param request
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Patient patient, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    /**
     * 根据id删除就诊人信息
     * @param id
     * @return
     */
    @DeleteMapping("/removePatient/{id}")
    public R removePatient(@PathVariable Long id){
        patientService.removeById(id);
        return R.ok();
    }

    /**
     * 修改用户之回显用户信息
     * @param id
     * @return
     */
    @GetMapping("/findPatientById/{id}")
    public R findPatientById(@PathVariable Long id){
        Patient patient = patientService.findPatientById(id);
        return R.ok().setData("patient",patient);
    }

    /**
     * 修改用户之真正修改
     * @param patient
     * @return
     */
    @PostMapping("/updatePatient")
    public R updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }

    /**
     * 查询所有的就诊人信息
     * @param request
     * @return
     */
    @GetMapping("/findAll")
    public R findAll(HttpServletRequest request){
        String token = request.getHeader("token");
        List<Patient> patientList = patientService.findPatientList(token);
        return R.ok().setData("patientList",patientList);
    }

    /**
     * 根据id查询就诊人信息
     * @param id
     * @return
     */
    @GetMapping("/findPatient/{id}")
    public Patient findPatient(@PathVariable("id")Long id){
        Patient patient = patientService.findPatientById(id);
        return patient;
    }
}
